package com.correo.vista.server.windows;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.correo.controlador.server.beans.UserDBBean;
import com.correo.database.beans.CasillaCorreoBean;
import com.correo.database.beans.IBean;
import com.correo.database.beans.OfficeBean;
import com.correo.database.beans.UserBean;
import com.correo.exceptions.server.RefreshException;
import com.correo.modelo.server.ModelServer;
import com.correo.socket.server.ServerSocketAdmin;
import com.correo.socket.server.run.ServerSocketThread;
import com.correo.vista.client.windows.componentes.paneles.PanelStatus;
import com.correo.vista.server.windows.componentes.menu.MenuAdministracionView;
import com.correo.vista.server.windows.componentes.paneles.PanelPrincipalServer;
import com.correo.vista.server.windows.componentes.paneles.PanelToolBarServer;
import com.correo.vista.server.windows.ventanasInternas.DialogCasillaCorreo;
import com.correo.vista.server.windows.ventanasInternas.DialogOficce;
import com.correo.vista.server.windows.ventanasInternas.DialogUser;

public class AdministracionCorreo extends JFrame {

	private static final long serialVersionUID = 1L;

	public static final int	TAB_USUARIO=0;
	public static final int	TAB_OFICINA_CORREO=1;
	public static final int	TAB_CASILLA_CORREO=2;
	public static final int	TAB_USUARIO_SOCKET=3;
	public static final int	TAB_RELACION_CONFIANZA=4;
	
	private JDesktopPane 			jContentPane;
	private MenuAdministracionView	menu;
	private PanelToolBarServer 		panelToolBar;  
	private PanelPrincipalServer 	panelPrincipal;  
	private PanelStatus 			panelStatus;  
	private ModelServer 			model;

	/**
	 * This is the default constructor
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws RefreshException 
	 */
	public AdministracionCorreo() throws RefreshException, SQLException, ClassNotFoundException {
		super();
		jContentPane = null;
		panelToolBar = null;
		panelPrincipal = null;
		panelStatus = null;
		menu=null;
		initialize();
	}

	public AdministracionCorreo(ModelServer model) throws RefreshException, SQLException, ClassNotFoundException {
		super();
		jContentPane = null;
		panelToolBar = null;
		panelPrincipal = null;
		panelStatus = null;
		menu=null;
		this.model = model;
		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws RefreshException 
	 */
	private void initialize() throws RefreshException, SQLException, ClassNotFoundException {
		iniciarSocketServer();
		this.setSize(300, 200);
		
		this.setJMenuBar(getMenu().getMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("Administracion");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setVisible(true);
		
	}

	private void iniciarSocketServer() {
		Thread thread = new Thread(new ServerSocketThread(), "Socket Server");
		thread.setName("Socket Server");
		thread.start();
		
	}

	public MenuAdministracionView getMenu(){
		if(menu==null){
			menu = new MenuAdministracionView(model);
		}
		return menu;
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws RefreshException 
	 */
	private JDesktopPane getJContentPane() throws RefreshException, SQLException, ClassNotFoundException {
		if (jContentPane == null) {
			jContentPane = new JDesktopPane();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPanelToolBar(),BorderLayout.NORTH);
			jContentPane.add(getPanelPrincipal(),BorderLayout.CENTER);
			jContentPane.add(getPanelStatus(),BorderLayout.SOUTH);			
		}
		return jContentPane;
	}

	private JPanel getPanelToolBar() throws RefreshException, SQLException, ClassNotFoundException {
		if (panelToolBar == null) {
			panelToolBar = new PanelToolBarServer(getJContentPane(),model,this);
			//panelToolBar.setBorder(BorderFactory.createLineBorder(Color.magenta, 5));
		}
		return panelToolBar.getPanelToolbar();
	}

	public PanelToolBarServer getToolBar(){
		return panelToolBar;
	}
	/**
	 * This method initializes panelPrincipal	
	 * 	
	 * @return javax.swing.JPanel	
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws RefreshException 
	 */
	private JPanel getPanelPrincipal() throws RefreshException, SQLException, ClassNotFoundException {
		if (panelPrincipal == null) {
			panelPrincipal = new PanelPrincipalServer(this);
			//panelPrincipal.setBackground(Color.BLUE);
		}
		return panelPrincipal.getPanelPrincipal();
	}	
	private  PanelPrincipalServer getPrincipal() throws RefreshException, SQLException, ClassNotFoundException {
		if (panelPrincipal == null) {
			panelPrincipal = new PanelPrincipalServer(this);
			//panelPrincipal.setBackground(Color.BLUE);
		}
		return panelPrincipal;
	}

	/**
	 * This method initializes panelStatus	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelStatus() {
		if (panelStatus == null) {
			panelStatus = new PanelStatus();
		}		
		//panelStatus.setBackground(Color.BLUE);
		return panelStatus.getStatusPanel();
	}

	public void setPanelToolBar(PanelToolBarServer panelToolBar) {
		this.panelToolBar = panelToolBar;
	}

	public void setPanelPrincipal(PanelPrincipalServer panelPrincipal) {
		this.panelPrincipal = panelPrincipal;
	}

	public void setPanelStatus(PanelStatus panelStatus) {
		this.panelStatus = panelStatus;
	}

	
	/////////////////Listeners
	public void setActionListenerAlta(ActionListener actionListener){
		getToolBar().setActionListenerAlta(actionListener);
		getMenu().setActionListenerAlta(actionListener);
	}
		
	public void setActionListenerBaja(ActionListener actionListener) {
		getToolBar().setActionListenerBaja(actionListener);
		getMenu().setActionListenerBaja(actionListener);
	}
	public void setActionListenerModificacion(ActionListener actionListener) {
		getToolBar().setActionListenerModificacion(actionListener);
		getMenu().setActionListenerModificacion(actionListener);
	}
	public void setActionListenerView(ActionListener actionListener) {
		getToolBar().setActionListenerView(actionListener);
		getMenu().setActionListenerView(actionListener);
	}
	public void setActionListenerActualizar(ActionListener actionListener) {
		getToolBar().setActionListenerActualizar(actionListener);
		getMenu().setActionListenerActualizar(actionListener);
	}

	
//	/*
//	 *Se realiza el alta de los usuarios en la tabla ( tabla de usuarios DB)
//	 */
//	public void altaUserDB() {
//		getToolBar().getDialogAddUserDB().setVisible(false);
//		//TODO aca se actualiza la tabla donde estan los usuarios
//		getPrincipal().agregarUsuario(getUserDB());//obtengo el nuevo usuario.
//		
//	}
//	public void modificacionUserDB() {
//		getToolBar().getDialogAddUserDB().setVisible(false);
//		//TODO aca se actualiza la tabla donde estan los usuarios
//		getPrincipal().modificarUsuario(getUserDB());//obtengo el nuevo usuario.
//		
//	}

	/*
	 * obtiene el numero de tab.
	 * */
	public int getTab() throws RefreshException, SQLException, ClassNotFoundException {		
		return getPrincipal().getSelectionTab();
	}

	public void bajaUserDB() throws RefreshException, SQLException, ClassNotFoundException {		
		getPrincipal().borrarUsuario();//borro al usuario.
		
	}

	

	public void actualizarTablaUserDB(List<UserDBBean> listusersDB) throws RefreshException, SQLException, ClassNotFoundException {
		this.getPrincipal().actualizarTablaDB(listusersDB);
		
	}

	

	public DialogOficce getDialogOficce(OfficeBean officeBean,boolean modify,boolean view) throws RefreshException, SQLException, ClassNotFoundException{
		return getToolBar().getDialogOffice(officeBean,modify,view);
	}
	public DialogUser getDialogUser(UserBean user,boolean modify,boolean view) {

		return getToolBar().getDialogUser(user,modify,view);
	}
	public DialogCasillaCorreo getDialogCasillaCorreo(CasillaCorreoBean casilla, boolean modify, boolean view) throws RefreshException, SQLException, ClassNotFoundException {

		return getToolBar().getDialogCasillaCorreo(casilla,modify,view);
	}
	public void actualizacion() {
		// TODO Auto-generated method stub
		
	}

	public List<IBean> getOficceSelectionTabla() throws RefreshException, SQLException, ClassNotFoundException {
		
		return getPrincipal().getSelectionOfficeTabla();
	}
	public List<IBean> getUserSelectionTabla() throws RefreshException, SQLException, ClassNotFoundException {
		
		return getPrincipal().getSelectionUserTabla();
	}
	public List<IBean> getCasillaCorreoSelectionTabla() throws RefreshException, SQLException, ClassNotFoundException {
		return getPrincipal().getSelectionCasillaCorreoTabla();
	}

	public void actualizarTabla(List<IBean> lista) throws RefreshException, SQLException, ClassNotFoundException {
		getPrincipal().actualizar(lista);
		
	}

	public ModelServer getModel() {
		return model;
	}

	public void setModel(ModelServer model) {
		this.model = model;
	}

	public void actualizarTablaUsuario(List<IBean> refresh) throws RefreshException, SQLException, ClassNotFoundException {
		getPrincipal().actualizarUsuario(refresh);
		
	}

	public void actualizarTablaCasillaCorreo(List<IBean> refresh) throws RefreshException, SQLException, ClassNotFoundException {
		getPrincipal().actualizarCasillaCorreo(refresh);
	}

	public IBean getDataDialogOficce() throws RefreshException, SQLException, ClassNotFoundException {
		// TODO Auto-generated method stub
		return getDialogOficce().getData();
	}
	public  DialogOficce getDialogOficce() throws RefreshException, SQLException, ClassNotFoundException {
		// TODO Auto-generated method stub
		return  getToolBar().getDialogOffice();
	}
	

	

	
	
}
